<?php
	class Loaiphong_model extends CI_Model 
	{
    	function LayTatCaLoaiPhong_KhachSan() 
		{
			$post_per_page = 20;
        	$num = 0;
	        if ($this->uri->segment(3))
    	        $num = $this->uri->segment(3);

        	$strSQL = "SELECT lp.*, ks.Ten AS TenKS, ncc.TinhTrangTaiKhoan
						FROM LoaiPhong lp
						JOIN KhachSan ks ON lp.KhachSan = ks.MaKS
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
						ORDER BY ks.Ten ASC
						LIMIT $num, $post_per_page";
	        $query = $this->db->query($strSQL);

    	    $total_query = "SELECT lp.*, ks.Ten AS TenKS, ncc.TinhTrangTaiKhoan
							FROM LoaiPhong lp
							JOIN KhachSan ks ON lp.KhachSan = ks.MaKS
							JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
							WHERE ncc.TinhTrangTaiKhoan IS NOT NULL";

        	$this->load->library('pagination');
	        $config['base_url'] = base_url() . 'index.php/loaiphong/admin_index';
    	    $config['total_rows'] = $this->db->query($total_query)->num_rows();
        	$config['per_page'] = $post_per_page;
	        $this->pagination->initialize($config);

    	    if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function Tim($key) 
		{
        	$post_per_page = 20;
	        $num = 0;
    	    if ($this->uri->segment(4))
        	    $num = $this->uri->segment(4);

	        $strSQL = "SELECT lp.*, ks.Ten AS TenKS, ncc.TinhTrangTaiKhoan
						FROM LoaiPhong lp
						JOIN KhachSan ks ON lp.KhachSan = ks.MaKS
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
						AND (lp.MaLP = '$key' OR lp.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')
						ORDER BY ks.Ten ASC
						LIMIT $num, $post_per_page";
    	    $query = $this->db->query($strSQL);

        	$total_query = "SELECT lp.*, ks.Ten AS TenKS, ncc.TinhTrangTaiKhoan
							FROM LoaiPhong lp
							JOIN KhachSan ks ON lp.KhachSan = ks.MaKS
							JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
							WHERE ncc.TinhTrangTaiKhoan IS NOT NULL
							AND (lp.MaLP = '$key' OR lp.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')";

	        $this->load->library('pagination');
    	    $config['uri_segment'] = 4;
        	$config['base_url'] = base_url() . 'index.php/loaiphong/Tim/' . $key;
	        $config['total_rows'] = $this->db->query($total_query)->num_rows();
    	    $config['per_page'] = $post_per_page;
        	$this->pagination->initialize($config);

	        if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}
		
		function LayTenKhachSan_ajax($key)
		{
        	$this->db->select('KhachSan.Ten');
	        $this->db->from('LoaiPhong');
    	    $this->db->join('KhachSan', 'LoaiPhong.KhachSan = KhachSan.MaKS');
        	$this->db->like('KhachSan.Ten', $key);
			$this->db->order_by('KhachSan.Ten', "asc");	
			$this->db->distinct();		
	        $query = $this->db->get();
    	    if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;			
		}
		
		function LayMaLP_ajax($key)
		{
        	$this->db->select('LoaiPhong.MaLP AS Ten');
	        $this->db->from('LoaiPhong');
        	$this->db->like('LoaiPhong.MaLP', $key);
			$this->db->order_by('LoaiPhong.MaLP', "asc");				
			$this->db->distinct();						
	        $query = $this->db->get();
    	    if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;			
		}
		
		function LayTenLP_ajax($key)
		{
        	$this->db->select('LoaiPhong.Ten');
	        $this->db->from('LoaiPhong');
        	$this->db->like('LoaiPhong.Ten', $key);
			$this->db->order_by('LoaiPhong.Ten', "asc");				
			$this->db->distinct();					
	        $query = $this->db->get();
    	    if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;			
		}		

	    function LayLoaiPhong_KhachSan($malp) 
		{
        	$this->db->select('LoaiPhong.*, KhachSan.Ten AS TenKS, KhachSan.QuocGia, KhachSan.Tinh_ThanhPho');
	        $this->db->from('LoaiPhong');
    	    $this->db->join('KhachSan', 'LoaiPhong.KhachSan = KhachSan.MaKS');
        	$this->db->where('LoaiPhong.MaLP', $malp);
	        $query = $this->db->get();
    	    if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function LayMaKS($malp) 
		{
        	$query = $this->db->get_where('LoaiPhong', array('MaLP' => $malp));
			
	        if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$maks = $row->KhachSan;
	            }
    	        return $maks;
        	}
	        return NULL;
    	}

	    function LayLoaiPhong_CuaMotKhachSan($maks) 
		{
        	$query = $this->db->get_where('LoaiPhong', array('KhachSan' => $maks));

	        if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function LayGiaThue($malp) 
		{
        	$query = $this->db->get_where('LoaiPhong', array('MaLP' => $malp));

        	if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$giathue = $row->GiaTruocKhuyenMai;
	            }
    	        return $giathue;
        	}
	        return NULL;
    	}

	    function Xoa($malp) 
		{
        	return $this->db->delete('LoaiPhong', array('MaLP' => $malp));
    	}

	    function Them($data) 
		{
        	$this->db->insert('LoaiPhong', $data);
	        return $this->db->affected_rows();
    	}

	    function CapNhat($data, $malp) 
		{
        	$this->db->update('LoaiPhong', $data, array('MaLP' => $malp));
	        return $this->db->affected_rows();
    	}

	    function get_records_loaiphong() 
		{
	        $loaiphong = $this->input->post('loaiphong');
    	    $this->db->select('MaLP, Ten, GiaTruocKhuyenMai, SoDemToiThieu, SoNguoiLonMacDinh, SoNguoiLonToiDa, SoNguoiToiDa, SoTienThemNguoiHienHanh');
        	$this->db->from('LoaiPhong');
	        $this->db->where('MaLP', $loaiphong);

    	    $q = $this->db->get();
        	if ($q->num_rows() > 0) 
			{
            	foreach ($q->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	    }

    	function layloaiphong($makhachsan) 
		{
        	$this->db->select('MaLP, Ten, GiaTruocKhuyenMai, SoDemToiThieu, SoNguoiLonMacDinh, SoNguoiLonToiDa, SoNguoiToiDa');
	        $this->db->from('LoaiPhong');
    	    $this->db->where('KhachSan', $makhachsan);

        	$q = $this->db->get();
	        if ($q->num_rows() > 0) 
			{
            	foreach ($q->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	    }
	}
?>